package com.evil.application.enums.order;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * 订单查询类型
 *
 * @author 15587
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum StatisticsPendingTypeEnum {

    /**
     * 待办类型（0：全部待办，1：已超时，2：即将超时，3：催办）
     */
    ALL(0, "全部待办", OrderQueryTypeEnum.PENDING),
    TIMED_OUT(1, "已超时", OrderQueryTypeEnum.PENDING),
    TIMING_OUT(2, "即将超时", OrderQueryTypeEnum.PENDING),
    URGING(3, "催办", OrderQueryTypeEnum.PENDING),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final OrderQueryTypeEnum queryTypeEnum;

    public static Optional<StatisticsPendingTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static StatisticsPendingTypeEnum isExistById(Integer id) {
        return StatisticsPendingTypeEnum.findById(id).orElseThrow(StatisticsPendingTypeEnum::getBusinessException);
    }

    public static void forEach(Consumer<StatisticsPendingTypeEnum> action) {
        Arrays.asList(values()).forEach(action);
    }

    public static <R> Stream<R> stream(Function<StatisticsPendingTypeEnum, ? extends R> mapper) {
        return Arrays.stream(values()).map(mapper);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "待办类型参数错误");
    }
}
